/**
 * 
 */
package com.skood.common.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.skood.common.ExecuteResult;
import com.skood.common.dao.CommonDAO;
import com.skood.common.model.SearchModel;
import com.skood.platform.util.ServiceUtils;

/**
 * @author keege
 * @date 2014-1-5
 */
public abstract class AbstractService<T> implements Service<T> {
	private static Logger LOG = LoggerFactory.getLogger(AbstractService.class);
	
	protected abstract CommonDAO<T> getDAO();
	
	@Override
	public List<T> search(SearchModel<T> searchModel,
			Map<String, Object> parameters) {
		// TODO Auto-generated method stub
		String sortData = ServiceUtils.convertOrderSql("com.skood.security.dao.UserDAO.userMap", searchModel.getSort());
		searchModel.setSorterData(sortData);
		return this.getDAO().search(searchModel, parameters);
	}

	@Override
	public Long searchCount(SearchModel<T> searchModel,
			Map<String, Object> parameters) {
		// TODO Auto-generated method stub
		return this.getDAO().searchCount(searchModel, parameters);
	}
	@Override
	public List<T> search(SearchModel<T> searchModel) {
		// TODO Auto-generated method stub
		return search(searchModel,null);
	}

	@Override
	public Long searchCount(SearchModel<T> searchModel) {
		// TODO Auto-generated method stub
		return searchCount(searchModel,null);
	}

	@Override
	public ExecuteResult<T> doDelete(Long[] ids) {

		// TODO Auto-generated method stub
		ExecuteResult<T> executeResult = new ExecuteResult<T>();
		if(ArrayUtils.isEmpty(ids)){
			LOG.info("要删除的数据参数不能为空");
			executeResult.addErrorMessages("要删除的数据参数不能为空");
			return executeResult;
		}
		try{
			getDAO().delete(ids);
		}catch(RuntimeException e){
			LOG.error("删除数据异常,参数ids={},{}",ids,e.getMessage());
			throw e;
		}
		
		return executeResult;
	}

}
